import { createStore } from "vuex";


export default createStore({
  state: {
    admin: {
      // 权限id集合
      permissionIds: [],
      // 身份令牌
      token: "",
    }
  },
  getters: {
    // 判断管理员是否拥有指定权限
    hasPermission: (state) => (id: number) => {
      const permissionIds = state.admin.permissionIds;
      if (permissionIds) {
        return permissionIds.some(v => v == id);
      }
    }
  },
  mutations: {
    // 保存管理员信息
    setAdmin(state, payload) {
      const admin = payload;
      admin.permissionIds = [];
      if (payload.permissionList) {
        payload.permissionList.forEach((item: any) => {
          item.permissionList.forEach((item2: any) => {
            admin.permissionIds.push(item2.permission_id);
          })
        });
      }
      state.admin = admin;
      console.log("State.admin = ", state.admin);
    },
  },
  actions: {},
  modules: {},
});

